package com.example.doer.reading.util;

import android.os.Environment;
import android.os.Message;

import com.example.doer.reading.MyReadTool.db.BookList;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import static java.lang.Thread.sleep;

/**
 * Created by 杨轩昂 on 2017/7/23.
 */
public class downtxt{
    private boolean downIsComplete=false;
    private boolean isSuccess=false;
    public void downTXT(final BookList bookList){

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL txtURL =new URL(bookList.getBookpath());
                    HttpURLConnection urlConn=(HttpURLConnection)txtURL.openConnection();
                    //得到输入流
                    InputStream inputStream = urlConn.getInputStream();
                    //获取自己数组
                    byte[] getData = readInputStream(inputStream);

                    //文件保存位置
                    String savePath= Environment.getExternalStorageDirectory().getPath()+"/ReadingTXT";
                    File saveDir = new File(savePath);
                    if (!saveDir.exists()) {
                        saveDir.mkdir();
                    }
                    File file = new File(savePath+"/"+bookList.getBookname()+".txt");
                    bookList.setBookpath(savePath+"/"+bookList.getBookname()+".txt");
                    bookList.setIfLocal(true);
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(getData);
                    Message message=new Message();
                    message.what=22222;
                    message.obj=
                    downIsComplete=true;
                    isSuccess=true;
                    if (fos != null) {
                        fos.close();
                    }
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    sleep(60000);
                    downIsComplete=true;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
    /**
     * 从输入流中获取字节数组
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
}
